package com.shanghaiyimu.yimiyangche.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.shanghaiyimu.yimiyangche.R;
import com.shanghaiyimu.yimiyangche.activity.GoodsListActivity;
import com.shanghaiyimu.yimiyangche.adapter.GridAdapter;
import com.shanghaiyimu.yimiyangche.bean.PersionInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * 车品商城  右边Fragment
 */
public class MyFragment extends Fragment {


    public MyFragment() {
        // Required empty public constructor
    }

    private static RecyclerView recyclerview;
    private GridAdapter mAdapter;
    private List<String> names = new ArrayList<>();
    private GridLayoutManager mLayoutManager;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_my, container, false);

        initView(view);
        initData();
        return view;
    }

    private void initView(View view) {
        TextView tv_title = (TextView) view.findViewById(R.id.tv_title);
        //得到数据
        PersionInfo info = (PersionInfo) getArguments().getSerializable("info");
        tv_title.setText(info.getNameString());

        recyclerview = (RecyclerView) view.findViewById(R.id.grid_recycler);
        mLayoutManager=new GridLayoutManager(getContext(),3,GridLayoutManager.VERTICAL,false);//设置为一个3列的纵向网格布局
        recyclerview.setLayoutManager(mLayoutManager);
        mAdapter = new GridAdapter(getContext(),names);
        recyclerview.setAdapter(mAdapter);

        mAdapter.setOnItemClickListener(new GridAdapter.ViewHolder.MyItemClickListener() {
            @Override
            public void onItemClick(View view, int postion) {
                Toast.makeText(getContext(),postion+"",Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(getActivity(), GoodsListActivity.class);
//                intent.putExtra("name","货物1");
                startActivity(intent);
            }
        });
    }

    private void initData() {

        for (int i = 0; i < 14; i++) {
            names.add("机油"+i);
        }
        mAdapter.notifyDataSetChanged();
    }

}
